package cn.lb.test.controller;

import cn.lb.test.service.IOrderService;
import cn.lb.test.domain.Order;
import cn.lb.test.query.OrderQuery;
import cn.lb.basic.util.R;
import cn.lb.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    public IOrderService orderService;


    /**
     * 保存和修改公用的
     * @param order  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public R addOrUpdate(@RequestBody Order order){
        try {
            if( order.getId()!=null)
                orderService.update(order);
            else
                orderService.insert(order);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public R delete(@PathVariable("id") Long id){
        try {
            orderService.deleteById(id);
            return R.me();
        } catch (Exception e) {
        e.printStackTrace();
            return R.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public R get(@PathVariable("id")Long id)
    {
        try {
            Order order = orderService.selectById(id);
            return R.me().setData(order);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public R list(){

        try {
            List< Order> list = orderService.selectAll();
            return R.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public R json(@RequestBody OrderQuery query)
    {
        try {
            PageList<Order> pageList = orderService.page(query);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
}
